package org.sanmao.wyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.sanmao.wyj.controller.utils.Result;
import org.sanmao.wyj.dao.ConmentDao;
import org.sanmao.wyj.domain.Conment;
import org.sanmao.wyj.domain.Order;
import org.sanmao.wyj.service.ConmentService;
import org.sanmao.wyj.service.OrderService;
import org.sanmao.wyj.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class ConmentServiceImpl extends ServiceImpl<ConmentDao, Conment> implements ConmentService {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ConmentDao conmentDao;

    @Override
    public int addCom(int oid, Conment conment, HttpServletRequest request) {
        Order order = orderService.getById(oid);
        Date date = new Date(System.currentTimeMillis());
        conment.setTime(date);
        conment.setOid(oid);
        String token = request.getHeader("token");
        int userId = JwtUtils.getUserId(token);
        conment.setUid(userId);
        if(conmentDao.insert(conment) > 0){
            int cid = conment.getCid();
            order.setState(4);
            order.setCid(cid);
            orderService.updateById(order);
            return 1;
        }
        else return 0;
    }
}
